package com.livesource.client.webservices.project;

import com.google.gwt.json.client.JSONObject;
import com.livesource.client.model.project.Project;
import com.livesource.client.utilities.ConvertJsonp;

public class ConvertJsonpToProject {

	public static Project convertJsonProject(JSONObject obj) {

		Project project = new Project();

		project.setProjectID(ConvertJsonp.convertToString(obj.get("ID")));

		project.setName(ConvertJsonp.convertToString(obj.get("name")));

		project.setRepositoryType(ConvertJsonp.convertToString(obj
				.get("repositoryType")));

		project.setRepositoryURL(ConvertJsonp.convertToString(obj
				.get("repositoryURL")));

		if (Project.REPOSITORY_TYPE_GITHUB.equals(project.getRepositoryType())) {

			getGithubProjectInfo(project);
		}

		return project;
	}

	public static void getGithubProjectInfo(Project project) {

		String[] projectSplitArray = project.getRepositoryURL().split("/");

		String username = projectSplitArray[projectSplitArray.length - 2];

		project.setGithubUserName(username);

		String projectName = projectSplitArray[projectSplitArray.length - 1];

		project.setGithubProjectName(projectName);
	}
}
